package com.ios.service.impl;

import com.ios.dao.RelativeLocationMapper;
import com.ios.dao.RelativeUserMapper;
import com.ios.dto.RelativeLocationExecution;
import com.ios.entity.RelativeLocation;
import com.ios.enums.LocationStateEnum;
import com.ios.service.IRelativeLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class RelativeLocationServiceImpl implements IRelativeLocationService{
    @Autowired
    private RelativeLocationMapper relativeLocationMapper;
    @Autowired
    private RelativeUserMapper relativeUserMapper;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public RelativeLocationExecution AddRelativeLocation(int relative_id, String latitude, String longitude) {
        if (relative_id==-1||latitude.equals(null)||longitude.equals(null)){
            return new RelativeLocationExecution(LocationStateEnum.NULL_PARAM);
        }else if (!CheckRelativeId(relative_id)){
            return new RelativeLocationExecution(LocationStateEnum.NULL_USER);
        }else {
            try{
                RelativeLocation relativeLocation = new RelativeLocation();
                relativeLocation.setRelativeId(relative_id);
                relativeLocation.setRelativeLongitude(longitude);
                relativeLocation.setRelativeLatitude(latitude);
                relativeLocation.setCreateTime(new Date());
                relativeLocationMapper.insert(relativeLocation);
                return new RelativeLocationExecution(LocationStateEnum.SUCCESS_ADD,relativeLocation);
            }catch (Exception e){
                return new RelativeLocationExecution(LocationStateEnum.FAIL_ADD);
            }
        }
    }

    @Override
    public RelativeLocationExecution QueryRelationLocation(int relative_id) {
        if (relative_id==-1){
            return new RelativeLocationExecution(LocationStateEnum.NULL_PARAM);
        }else if (!CheckRelativeId(relative_id)){
            return new RelativeLocationExecution(LocationStateEnum.NULL_USER);
        }else {
            try{
                int id = 0;
                RelativeLocation newrelativeLocation = new RelativeLocation();
                List<RelativeLocation> relativeLocations = relativeLocationMapper.selectByRelative(relative_id);
                for (RelativeLocation relativeLocation:relativeLocations){
                    Date maxdate = dateFormat.parse("2000-1-1");
                    if (relativeLocation.getCreateTime().compareTo(maxdate)>0){
                        maxdate = relativeLocation.getCreateTime();
                        id = relativeLocation.getId();
                    }
                }
                RelativeLocation relativeLocation = relativeLocationMapper.selectById(id);
                return new RelativeLocationExecution(LocationStateEnum.SUCCESS_QUERY,relativeLocation);
            }catch (Exception e){
                return new RelativeLocationExecution(LocationStateEnum.FAIL_QUERY);
            }
        }
    }

    /*判断用户是否存在*/
    private boolean CheckRelativeId(Integer relativeId){
        List<Integer > AllId = relativeUserMapper.queryAllId();
        for (Integer id : AllId){
            if (id==relativeId){
                return true;
            }
        }
        return false;
    }
}
